package com.lottery.im.module.service.ui;

import android.annotation.SuppressLint;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lottery.im.R;
import com.lottery.im.api.Api;
import com.lottery.im.api.result.entity.integral.CommodityDetail;
import com.lottery.im.api.result.entity.integral.ExchangeResult;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.presenter.CommodityDetailPresenter;
import com.lottery.im.module.service.view.CommodityDetailView;
import com.lottery.im.util.ToastUtils;
import com.lottery.im.util.rxbus.RxBus;
import com.lottery.im.widget.dialog.ExchangeDialogFragment;
import java.io.Serializable;

/**
 * 作者：cyy
 * 时间：2018/9/18。
 */
@SuppressLint("ValidFragment") public class CommodityDetailFragment
    extends BaseSwipeFragment<CommodityDetailPresenter>
    implements CommodityDetailView, ExchangeDialogFragment.ConfirmListener {
  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.img_commodity) ImageView imgCommodity;
  @BindView(R.id.tv_add) TextView tvAdd;
  @BindView(R.id.tv_buyNum) TextView tvBuyNum;
  @BindView(R.id.tv_sub) TextView tvSub;
  @BindView(R.id.btn_exchange) Button btnExchange;
  @BindView(R.id.tv_commodityName) TextView tvCommodityName;
  @BindView(R.id.tv_stock) TextView tvStock;
  @BindView(R.id.tv_price) TextView tvPrice;
  @BindView(R.id.webView) WebView webView;

  private int id;
  private double amount;//当前积分
  private int buyCount = 1;//购买数量
  private int mPrice = 100000000;//所需积分
  private int mSumMoney;//购买总金额
  private String mCommodityName;

  @SuppressLint("ValidFragment") public CommodityDetailFragment(int id, double amount) {
    this.id = id;
    this.amount = amount;
  }

  @Override protected int getLayoutResId() {
    return R.layout.fragment_commodity_detail;
  }

  @Override protected void initPresenter() {
    presenter = new CommodityDetailPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    RxBus.get().register(this);
    tvTitle.setText("积分兑换");
    presenter.queryCommodityDetail(id);
  }

  @Override public void loadCommodityDetailSuccess(CommodityDetail commodityDetail) {
    tvCommodityName.setText(mCommodityName = commodityDetail.getC_name());
    tvPrice.setText((mPrice = commodityDetail.getPrice()) + "");
    tvStock.setText("库存" + commodityDetail.getStock() + "件");

    Glide.with(getActivity())
        .load(Api.BASEADDRESS + commodityDetail.getImg())
        .apply(new RequestOptions()
            .error(R.drawable.icon_conversation_de)
            .placeholder(R.drawable.icon_conversation_de)
            .fitCenter())
        .into(imgCommodity);

    webView.loadDataWithBaseURL(null, commodityDetail.getDetail(),
        "text/html", "UTF-8", null);

    if (amount < mPrice) {
      btnExchange.setEnabled(false);
      btnExchange.setBackgroundColor(getResources().getColor(R.color.background_gray3));
      btnExchange.setText("不可兑换");
    }
    mSumMoney = mPrice * buyCount;
  }

  @OnClick({ R.id.iv_back, R.id.tv_add, R.id.tv_sub, R.id.btn_exchange })
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_add:
        if (amount >= (mSumMoney + mPrice)) {
          buyCount++;
          mSumMoney = mPrice * buyCount;
          tvBuyNum.setText(buyCount + "");
        } else {
          ToastUtils.showToast(getActivity(), "当前积分不足");
        }
        break;
      case R.id.tv_sub:
        if (buyCount > 0) {
          buyCount--;
          tvBuyNum.setText(buyCount + "");
        }
        break;
      case R.id.btn_exchange:
        if (buyCount <= 0) {
          ToastUtils.showToast(getActivity(), "请添加商品数量");
          return;
        }
        ExchangeMessage exchangeMessage =
            new ExchangeMessage(id, mCommodityName, buyCount, mSumMoney);
        ExchangeDialogFragment fragment = ExchangeDialogFragment.newInstance(exchangeMessage);
        fragment.show(getActivity().getSupportFragmentManager(), null);
        fragment.setOnConfirmListener(this);
        break;
    }
  }

  @Override public void onConfirmListener(ExchangeResult result) {
    startFragmentAndDestroyCurrent(new ExchangeResultFragment(result));
  }

  public static class ExchangeMessage implements Serializable {
    private int id;
    private String commodityName;
    private int count;
    private int monty;

    public ExchangeMessage(int id, String commodityName, int count, int monty) {
      this.id = id;
      this.commodityName = commodityName;
      this.count = count;
      this.monty = monty;
    }

    public int getId() {
      return id;
    }

    public void setId(int id) {
      this.id = id;
    }

    public String getCommodityName() {
      return commodityName;
    }

    public void setCommodityName(String commodityName) {
      this.commodityName = commodityName;
    }

    public int getCount() {
      return count;
    }

    public void setCount(int count) {
      this.count = count;
    }

    public int getMonty() {
      return monty;
    }

    public void setMonty(int monty) {
      this.monty = monty;
    }
  }
}
